<template>
  <slot name="elem" :form="form"></slot>
  <div @click="onsubmit">
    <div v-if="$slots.submit">
      <slot name="submit"></slot>
    </div>
    <div v-else>
      <div>没有使用</div>
    </div>
  </div>
</template>
<script>
  import Validator from 'async-validator';
  import 'weui'
  import weui from 'weui.js'
  export default {
    props: {
      rules: {
        type: Object,
        default: () => {},
      },
    },
    data() {
      return {
        form: {
          a: 1,
          b: 2,
          c: 3,
        },
      };
    },
    methods: {
      check(form) {
        let validator = new Validator(this.rules);
        return new Promise((resolve, reject) => {
          validator.validate(form, (errors, fields) => {
            if (errors) {
              reject(errors);
            } else {
              resolve();
            }
          });
        });
      },
      onsubmit() {
        this.check(this.form).then(res => {
          console.log(this.form);
        },err => {
          weui.topTips(err[0].message);
          console.log(err);
        })
      },
    },
    mounted(){
      console.log(this.$slots);
    }
  };
</script>
